package com.ffapp.rainmonitorcustom.alarm;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.ffapp.rainmonitorcustom.R;
import com.ffapp.rainmonitorcustom.basis.BasisActivity;
import com.ffapp.rainmonitorcustom.basis.BasisApp;
import com.ffapp.rainmonitorcustom.basis.Constants;
import com.ffapp.rainmonitorcustom.bean.AlarmListData;
import com.loopj.android.http.RequestParams;

import butterknife.BindView;
import butterknife.OnClick;
import my.ActivityTool;
import my.DialogUtils;
import my.http.HttpRestClient;
import my.http.MyHttpListener;

/**
 * 告警详情
 */
public class AlarmDetailActivity extends BasisActivity {


    AlarmListData alarmListData;
    AlarmListData mDetailBean;

    @BindView(R.id.text_title)
    public TextView text_title;
    @BindView(R.id.img_status)
    public ImageView img_status;
    @BindView(R.id.img_photo)
    public ImageView img_photo;

    @BindView(R.id.text_status)
    public TextView text_status;
    @BindView(R.id.text_device_name)
    public TextView text_device_name;
    @BindView(R.id.text_dept)
    public TextView text_dept;
    @BindView(R.id.text_alarmtime)
    public TextView text_alarmtime;
    @BindView(R.id.text_device_num)
    public TextView text_device_num;
    @BindView(R.id.text_device_address)
    public TextView text_device_address;
    @BindView(R.id.text_device_position)
    public TextView text_device_position;


    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.alarm_detail_activity);

        setTitle("告警详情");
        setTitleLeftButton(null);

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);

        alarmListData = (AlarmListData) getIntent().getExtras().getSerializable("data");
        if(alarmListData == null){
            finish();
            return;
        }
        getDetail();
    }


    private void getDetail() {
        RequestParams params = new RequestParams();
        showProgress();
        params.put("no", alarmListData.getNO());
        params.put("id", alarmListData.getID());
        params.put("deviceType", alarmListData.getDEVICETYPE());
        HttpRestClient.post(Constants.URL_ALARM_DETAIL, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {

                        mDetailBean = (AlarmListData) result;
//                        alarmListData.mergListData(mDetailBean);
//                        mDetailBean = alarmListData;
                        setDetailView();
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        dismissProgress();
                    }
                },
                0, AlarmListData.class);

    }

    void setDetailView() {
        if(isFinishing())return;
        if (mDetailBean == null) return;

        BasisApp.loadImg(mContext, mDetailBean.getIMG_Real(), img_photo, R.drawable.base_img_default_square);
        text_title.setText(mDetailBean.getALARM());
        text_status.setText(mDetailBean.getSTATUSStr());
        img_status.setImageResource(mDetailBean.getSTATUS_resid());

        text_device_name.setText(mDetailBean.getNAME());
        text_dept.setText(mDetailBean.getOFFCOMNAME());
        text_alarmtime.setText(mDetailBean.getGATHERTIME());
        text_device_num.setText(mDetailBean.getNO());
        text_device_address.setText(mDetailBean.getADDRESS());
        text_device_position.setText(mDetailBean.getPOSITION());


    }


    @OnClick(R.id.btn_ok)
    void handle() {
//        PopHandle popFilter = new PopHandle(mContext);
//        popFilter
//                .showAnim(null)
//                .dismissAnim(null)
//                .dimEnabled(true)
//                .show();
        if(mDetailBean == null)return;
        if(mDetailBean.getPROCESSINGSTATUS() == 3){
            DialogUtils.DialogOkMsg(mContext,"火警状态无法重新处理！");
            return;
        }

        Bundle extras = new Bundle();
        extras.putSerializable("data", mDetailBean);
        ActivityTool.skipActivityForResult(mContext, AlarmDetailProcessActivity.class, extras, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        setResult(resultCode);
        if (resultCode == Activity.RESULT_OK) {
//            getDetail();
            finish();
        }
    }
}
